/*
 * WITNAFrameView.java
 *
 * Created on 15-Nov-2010, 20:08:19
 */
package editor.windows;

import adventure.Adventure;
import adventure.AdventureEdit;
import adventure.AdventureImp;
import adventure.commands.skills.SkillManager;
import adventure.placeable.PlaceableManager;
import adventure.room.RoomManagerEdit;
import console.gui.windows.About;
import editor.*;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
import java.text.NumberFormat;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author michaelhanns
 */
public class WITNAFrameView extends javax.swing.JFrame implements WITNAEditor {

    private static final long serialVersionUID = -2269398008221474366L;
    private double version;
    private AdventureEdit activeAdv;
    private RoomManagerEdit rooms;
    private BufferedImage offScreen;
    private Graphics2D g;
    private boolean roomSelected;
    private int[] copyRoom;
    private File openFile;
    private boolean unsavedChanges;
    private boolean standalone;
    private Random random;
    private double lastVersion = 0;

    /**
     * Creates new form WITNAFrameView
     */
    public WITNAFrameView(boolean standAlone, double softwareVersion) {
        initComponents();
        this.version = softwareVersion;
        random = new Random();

        setUpScrollPane();
        offScreen = new BufferedImage(5000, 5000, 1);
        g = offScreen.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        copyRoom = new int[3];
        openFile = null;
        setUnsavedChanges(false);
        setIconImage(new ImageIcon(getClass().getResource("/console/witnalogo.png")).getImage());
        newAdventure();
        toFront();

        standalone = standAlone;

        this.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent e) {
                if (openFile != null && console.getText().length() > 0) {
                    dumpChangeLog();
                }
                closeDialog();
            }
        });
        new PopUp().create(this);
    }

    @Override
    public void setSpawn(int x, int y, int z) {
        if (activeAdv.sx() != x || activeAdv.sy() != y || activeAdv.sz() != z) {
            if (rooms.roomExists(x, y, z)) {
                activeAdv.setStartArea(x, y, z);
                printToConsole("Spawn location updated to (" + x + ", " + y + ", " + z + ").");
            } else {
                printToConsole("You must select an existing room to set as initial spawn!");
            }
        }
    }

    public int getCloseOperation() {
        return javax.swing.WindowConstants.DISPOSE_ON_CLOSE;
    }

    private void setUpScrollPane() {
        mapScrollPane.getVerticalScrollBar().setValue(5000);
        mapScrollPane.getVerticalScrollBar().setUnitIncrement(50);
        mapScrollPane.getHorizontalScrollBar().setUnitIncrement(50);

        AdjustmentListener drawWhenScrolling = new AdjustmentListener() {
            @Override
            public void adjustmentValueChanged(AdjustmentEvent e) {
                drawBuffer();
            }
        };

        mapScrollPane.getHorizontalScrollBar().addAdjustmentListener(drawWhenScrolling);
        mapScrollPane.getVerticalScrollBar().addAdjustmentListener(drawWhenScrolling);
    }

    public void setPopUpListener(MouseListener listener) {
        mapPanel.addMouseListener(listener);
    }

    @Override
    public int getFloor() {
        return Integer.parseInt(floorSpinner.getValue().toString());
    }

    public void setUnsavedChanges(boolean unsaved) {
        if (!unsavedChanges) {
            this.unsavedChanges = unsaved;
            if (unsavedChanges) {
                setTitle(getTitle() + "*");
            }
        } else {
            this.unsavedChanges = unsaved;

            if (!unsavedChanges && getTitle().endsWith("*")) {
                setTitle(getTitle().substring(0, getTitle().length() - 1));
            }
        }
    }

    private void newAdventure() {
        openFile = null;
        String[] titles = {"MegaQuest 2: Back To The MegaDome",
            "Escape From Castle Lederhosen", "Adventure Grind",
            "Builder's Gates", "Mass Reaction", "ROGUE2: Courier New",
            "Dungeons and Daggers", "final static Fantasy XXIV",
            "Castlemania", "Younger Scrolls", "Dragon Chore"};
        String newTitle = titles[random.nextInt(titles.length)];

        if (activeAdv != null) {
            while (activeAdv.getName().equals(newTitle)) {
                newTitle = titles[random.nextInt(titles.length)];
            }
        }

        activeAdv = new AdventureImp(newTitle, version);
        clearFields();
        rooms = activeAdv.getRoomManager();
        drawFloorToBuffer();
        setTitle("WITNA Editor v" + version + " [" + activeAdv.getName() + "]");
        console.setText("");
        console.setCaretPosition(0);
        printToConsole("New adventure " + activeAdv.getName() + " created!");
        roomSelected = false;
        copyRoom[0] = -1;
        copyRoom[1] = -1;
        copyRoom[2] = -1;
        unsavedChanges = false;
        lastVersion = 0.0;
    }

    public void getRoomDetails(int x, int y, int z) {
        xField.setText(x + "");
        yField.setText(y + "");

        if (activeAdv.getRoomManager().roomExists(x, y, z)) {
            String field = rooms.getRoomLitTitle(x, y, z);
            titleField.setText(field);

            field = rooms.getRoomLitDescription(x, y, z);
            descriptionField.setText(field);
            descriptionField.setCaretPosition(0);

            field = rooms.getClosed(x, y, z);
            closedN.setSelected(false);
            closedE.setSelected(false);
            closedS.setSelected(false);
            closedW.setSelected(false);
            closedU.setSelected(false);
            closedD.setSelected(false);

            for (int c = 0; c < field.length(); c++) {
                if (field.charAt(c) == 'N') {
                    closedN.setSelected(true);
                }
                if (field.charAt(c) == 'E') {
                    closedE.setSelected(true);
                }
                if (field.charAt(c) == 'S') {
                    closedS.setSelected(true);
                }
                if (field.charAt(c) == 'W') {
                    closedW.setSelected(true);
                }
                if (field.charAt(c) == 'U') {
                    closedU.setSelected(true);
                }
                if (field.charAt(c) == 'D') {
                    closedD.setSelected(true);
                }
            }
            field = rooms.getBlocked(x, y, z);
            blockedN.setSelected(false);
            blockedE.setSelected(false);
            blockedS.setSelected(false);
            blockedW.setSelected(false);
            blockedU.setSelected(false);
            blockedD.setSelected(false);

            for (int c = 0; c < field.length(); c++) {
                if (field.charAt(c) == 'N') {
                    blockedN.setSelected(true);
                }
                if (field.charAt(c) == 'E') {
                    blockedE.setSelected(true);
                }
                if (field.charAt(c) == 'S') {
                    blockedS.setSelected(true);
                }
                if (field.charAt(c) == 'W') {
                    blockedW.setSelected(true);
                }
                if (field.charAt(c) == 'U') {
                    blockedU.setSelected(true);
                }
                if (field.charAt(c) == 'D') {
                    blockedD.setSelected(true);
                }
            }

            field = rooms.getLocked(x, y, z);
            lockedN.setSelected(false);
            lockedE.setSelected(false);
            lockedS.setSelected(false);
            lockedW.setSelected(false);
            lockedU.setSelected(false);
            lockedD.setSelected(false);

            for (int c = 0; c < field.length(); c++) {
                if (field.charAt(c) == 'N') {
                    lockedN.setSelected(true);
                }
                if (field.charAt(c) == 'E') {
                    lockedE.setSelected(true);
                }
                if (field.charAt(c) == 'S') {
                    lockedS.setSelected(true);
                }
                if (field.charAt(c) == 'W') {
                    lockedW.setSelected(true);
                }
                if (field.charAt(c) == 'U') {
                    lockedU.setSelected(true);
                }
                if (field.charAt(c) == 'D') {
                    lockedD.setSelected(true);
                }
            }

            field = "" + rooms.getCurrencyContents(x, y, z);
            goldField.setText(field);

            field = "" + rooms.getSpawnLevel(x, y, z);
            spawnField.setText(field);

            field = "" + rooms.getTicksUntilRepop(x, y, z);
            repopField.setText(field);

            lowLightCheckBox.setSelected(rooms.getRoomLowLight(x, y, z));

            javax.swing.DefaultListModel model = new javax.swing.DefaultListModel();
            for (int c = 0; rooms.getItemIDAtPosition(x, y, z, c) > 0; c++) {
                model.addElement(activeAdv.getPlaceableManager().getItem(rooms.getItemIDAtPosition(x, y, z, c)).getName());
            }
            itemsList.setModel(model);

            model = new javax.swing.DefaultListModel();
            for (int c = 0; rooms.getMobileIDAtPosition(x, y, z, c) > 0; c++) {
                model.addElement(activeAdv.getPlaceableManager().getMobile(rooms.getMobileIDAtPosition(x, y, z, c)).getName());
            }
            mobilesList.setModel(model);

            model = new javax.swing.DefaultListModel();
            for (int c = 0; rooms.getTriggerIDAtPosition(x, y, z, c) > 0; c++) {
                model.addElement(activeAdv.getPlaceableManager().getTrigger(rooms.getTriggerIDAtPosition(x, y, z, c)).getName());
            }
            triggersList.setModel(model);
        } else {
            clearFields();
        }
    }

    private void updateRoomDetails(int x, int y, int z) {
        if (rooms.roomExists(x, y, z)) {
            String field = titleField.getText();
            System.out.println(field);
            rooms.editRoom(x, y, z, "TITLE", field);

            field = descriptionField.getText();
            rooms.editRoom(x, y, z, "DESCRIPTION", field);

            field = getClosedString();
            rooms.editRoom(x, y, z, "CLOSED", field);

            field = getLockedString();
            rooms.editRoom(x, y, z, "LOCKED", field);

            field = getBlockedString();
            rooms.editRoom(x, y, z, "BLOCKED", field);

            boolean selected = lowLightCheckBox.isSelected();
            rooms.editRoom(x, y, z, "LOWLIGHT", selected);

            int spawn = parseInt(spawnField.getText());
            rooms.editRoom(x, y, z, "SPAWN", spawn);

            if (spawn > 0) {
                boolean aggroMobs = false;
                int mobID = -1;
                for (int c = 0; (mobID = activeAdv.getRoomManager().getMobileIDAtPosition(x, y, z, c)) != 0; c++) {
                    if (activeAdv.getPlaceableManager().getMobile(mobID).isAggro()) {
                        aggroMobs = true;
                    }
                }
                if (aggroMobs) {
                    printToConsole("WARNING: Aggro mobile at spawn position "
                            + "(" + x + ", " + y + ", " + z + "). It is advised that "
                            + "spawn locations remain safe.");
                }
            }

            int gold = parseInt(goldField.getText());
            rooms.editRoom(x, y, z, "GOLD", gold);

            int repop = parseInt(repopField.getText());
            rooms.editRoom(x, y, z, "REPOP", repop);

            removeRoomOnBuffer(x, y, z);
            drawRoomToBuffer(x, y, z);
            drawBuffer();
            getRoomDetails(x, y, z);

            unsavedChanges = true;
        }
    }

    private String getClosedString() {
        String out = "";
        if (closedN.isSelected()) {
            out += "N";
        }
        if (closedE.isSelected()) {
            out += "E";
        }
        if (closedS.isSelected()) {
            out += "S";
        }
        if (closedW.isSelected()) {
            out += "W";
        }
        if (closedU.isSelected()) {
            out += "U";
        }
        if (closedD.isSelected()) {
            out += "D";
        }
        return out;
    }

    private String getLockedString() {
        String out = "";
        if (lockedN.isSelected()) {
            out += "N";
        }
        if (lockedE.isSelected()) {
            out += "E";
        }
        if (lockedS.isSelected()) {
            out += "S";
        }
        if (lockedW.isSelected()) {
            out += "W";
        }
        if (lockedU.isSelected()) {
            out += "U";
        }
        if (lockedD.isSelected()) {
            out += "D";
        }
        return out;
    }

    private String getBlockedString() {
        String out = "";
        if (blockedN.isSelected()) {
            out += "N";
        }
        if (blockedE.isSelected()) {
            out += "E";
        }
        if (blockedS.isSelected()) {
            out += "S";
        }
        if (blockedW.isSelected()) {
            out += "W";
        }
        if (blockedU.isSelected()) {
            out += "U";
        }
        if (blockedD.isSelected()) {
            out += "D";
        }
        return out;
    }

    private int parseInt(String text) {
        try {
            return Integer.parseInt(text);
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    private void clearFields() {
        titleField.setText("");
        descriptionField.setText("");

        lockedN.setSelected(false);
        lockedE.setSelected(false);
        lockedS.setSelected(false);
        lockedW.setSelected(false);
        lockedU.setSelected(false);
        lockedD.setSelected(false);

        blockedN.setSelected(false);
        blockedE.setSelected(false);
        blockedS.setSelected(false);
        blockedW.setSelected(false);
        blockedU.setSelected(false);
        blockedD.setSelected(false);

        closedN.setSelected(false);
        closedE.setSelected(false);
        closedS.setSelected(false);
        closedW.setSelected(false);
        closedU.setSelected(false);
        closedD.setSelected(false);

        goldField.setText("");
        spawnField.setText("");
        repopField.setText("");

        lowLightCheckBox.setSelected(false);

        javax.swing.DefaultListModel model = new javax.swing.DefaultListModel();
        itemsList.setModel(model);
        model = new javax.swing.DefaultListModel();
        mobilesList.setModel(model);
        model = new javax.swing.DefaultListModel();
        triggersList.setModel(model);
    }

    private String getMode() {
        String mode = "";
        if (newRadio.isSelected()) {
            mode = "NEW";
        } else if (selectRadio.isSelected()) {
            mode = "SELECT";
        } else if (deleteRadio.isSelected()) {
            mode = "DELETE";
        } else if (copyRadio.isSelected()) {
            mode = "COPY";
        }
        if (!selectRadio.isSelected()) {
            roomSelected = false;
        }

        if (!copyRadio.isSelected()) {
            copyRoom[0] = -1;
            copyRoom[1] = -1;
            copyRoom[2] = -1;
        }
        return mode;
    }

    public void drawBuffer() {
        if (mapPanel.getGraphics() != null) {
            // For x-to-y grid locations on floor 'floor', print the area boxes.
            int x = Math.max(0, mapScrollPane.getHorizontalScrollBar().getValue() - 250);
            int maxX = Math.min(5000, x + (mapScrollPane.getWidth()) + 250);
            int y = Math.max(0, mapScrollPane.getVerticalScrollBar().getValue() - 250);
            int maxY = Math.min(5000, y + (mapScrollPane.getHeight()) + 250);

            ((MapPanel) mapPanel).setDrawArea(x, y, maxX, maxY, offScreen, rootPane);
            mapPanel.repaint();
            //mapPanel.getGraphics().drawImage(offScreen, x, y, maxX, maxY, x, y, maxX, maxY, rootPane);
        }
    }

    public void drawFloorToBuffer() {
        if (g != null) {
            g.setColor(Color.black);
            g.fillRect(0, 0, 5000, 5000);
            int z = parseInt(floorSpinner.getValue().toString());
            for (int x = 0; x < 100; x++) {
                for (int y = 0; y < 100; y++) {
                    if (rooms.roomExists(x, y, z)) {
                        drawRoomToBuffer(x, y, z);
                    } else if(draggingRoom && x == dragRoom[0] && y == dragRoom[1] && z == dragRoom[2]) {
                        g.setColor(new Color(0, 150, 0));
                        g.drawRect(dragRoom[0] * 50 + 12, (99 - dragRoom[1]) * 50 + 12, 25, 25);
                    }
                }
            }
        }
    }

    private void drawRoomToBuffer(int x, int y, int z) {
        if (x < 100 && (99 - y) < 100 && z < 100) {
            // Draw the room square
            if (copyRoom[0] == x && copyRoom[1] == y && copyRoom[2] == z) {
                g.setColor(Color.CYAN);
            } else if (roomSelected && parseInt(xField.getText()) == x
                    && parseInt(yField.getText()) == y
                    && parseInt(floorSpinner.getValue() + "") == z) {
                g.setColor(Color.GREEN);
            } else if (draggingRoom && dragRoom[0] == x && dragRoom[1] == y && dragRoom[2] == z) {
                g.setColor(new Color(0, 150, 0));
            } else {
                g.setColor(Color.WHITE);
            }
            
            g.drawRect(x * 50 + 12, (99 - y) * 50 + 12, 25, 25);

            if (x == activeAdv.sx() && y == activeAdv.sy() && z == activeAdv.sz()) {
                g.setColor(Color.ORANGE);
                g.drawRect(x * 50 + 1, (99 - y) * 50 + 1, 48, 48);
            }

            // Draw the exits
            drawExits(x, y, z);
            drawExits(x - 1, y, z);
            drawExits(x + 1, y, z);
            drawExits(x, y - 1, z);
            drawExits(x, y + 1, z);
        }
    }

    private void removeRoomOnBuffer(int x, int y, int z) {
        if (x < 100 && (99 - y) < 100 && z < 100) {
            g.setColor(Color.BLACK);
            g.clearRect(x * 50, (99 - y) * 50, 51, 50);
        }
    }

    private void drawExits(int x, int y, int z) {
        g.setColor(Color.GRAY);
        if (x < 100 && y < 100 && z < 100) {
            if (rooms.roomExists(x, y, z)) {
                // Draw non-blocked paths..
                String bpaths = rooms.getBlocked(x, y, z);
                String lpaths = rooms.getLocked(x, y, z);
                String cpaths = rooms.getClosed(x, y, z);

                if (!bpaths.contains("S")) {
                    drawSouth(x, y, z);
                }
                if (!bpaths.contains("N")) {
                    drawNorth(x, y, z);
                }
                if (!bpaths.contains("W")) {
                    drawWest(x, y, z);
                }
                if (!bpaths.contains("E")) {
                    drawEast(x, y, z);
                }
                if (!bpaths.contains("U")) {
                    drawUp(x, y, z);
                }
                if (!bpaths.contains("D")) {
                    drawDown(x, y, z);
                }

                drawClosed(cpaths, x, y, z);
                drawLocked(lpaths, x, y, z);
            }
        }
    }

    private void drawSouth(int x, int y, int z) {
        // If there's another room and path's not blocked, draw the line.
        if (y > 0 && rooms.roomExists(x, y - 1, z)) {
            g.drawLine(x * 50 + 25, (99 - y) * 50 + 38,
                    x * 50 + 25, (99 - y) * 50 + 50);
        }
    }

    private void drawNorth(int x, int y, int z) {
        if (y < 99 && rooms.roomExists(x, y + 1, z)) {
            g.drawLine(x * 50 + 25, (99 - y) * 50,
                    x * 50 + 25, (99 - y) * 50 + 11);
        }
    }

    private void drawEast(int x, int y, int z) {
        if (x < 99 && rooms.roomExists(x + 1, y, z)) {
            g.drawLine(x * 50 + 50, (99 - y) * 50 + 25,
                    x * 50 + 38, (99 - y) * 50 + 25);
        }
    }

    private void drawWest(int x, int y, int z) {
        if (x > 0 && rooms.roomExists(x - 1, y, z)) {
            g.drawLine(x * 50 + 12, (99 - y) * 50 + 25,
                    x * 50, (99 - y) * 50 + 25);
        }
    }

    private void drawUp(int x, int y, int z) {
        if (z < 99 && rooms.roomExists(x, y, z + 1)) {
            //g.drawString("U", x * 50 + 15, (99 - y) * 50 + 30);
            //g.setColor(Color.white);
            g.drawLine(x * 50 + 18, (99 - y) * 50 + 20,
                    x * 50 + 18, (99 - y) * 50 + 30);
            g.drawLine(x * 50 + 18, (99 - y) * 50 + 20,
                    x * 50 + 14, (99 - y) * 50 + 24);
            g.drawLine(x * 50 + 18, (99 - y) * 50 + 20,
                    x * 50 + 22, (99 - y) * 50 + 24);
        }
    }

    private void drawDown(int x, int y, int z) {
        if (z > 0 && rooms.roomExists(x, y, z - 1)) {
            //g.drawString("D", x * 50 + 26,
            //      (99 - y) * 50 + 30);
            //g.setColor(Color.white);
            g.drawLine(x * 50 + 30, (99 - y) * 50 + 20,
                    x * 50 + 30, (99 - y) * 50 + 30);
            g.drawLine(x * 50 + 30, (99 - y) * 50 + 30,
                    x * 50 + 26, (99 - y) * 50 + 26);
            g.drawLine(x * 50 + 30, (99 - y) * 50 + 30,
                    x * 50 + 34, (99 - y) * 50 + 26);
        }
    }

    private void drawClosed(String cpaths, int x, int y, int z) {
        // Now see print closed door signifiers..
        g.setColor(Color.YELLOW);
        for (int n = 0; n < cpaths.length(); n++) {
            if (cpaths.charAt(n) == 'S') {
                g.drawLine(x * 50 + 18, (99 - y) * 50 + 16 + 25,
                        x * 50 + 32, (99 - y) * 50 + 16 + 25);
            } else if (cpaths.charAt(n) == 'N') {
                g.drawLine(x * 50 + 18, (99 - y) * 50 + 8,
                        x * 50 + 32, (99 - y) * 50 + 8);
            } else if (cpaths.charAt(n) == 'W') {
                g.drawLine(x * 50 + 8, (99 - y) * 50 + 18,
                        x * 50 + 8, (99 - y) * 50 + 32);
            } else if (cpaths.charAt(n) == 'E') {
                g.drawLine(x * 50 + 42, (99 - y) * 50 + 18,
                        x * 50 + 42, (99 - y) * 50 + 32);
            } else if (cpaths.charAt(n) == 'U') {
                g.drawLine(x * 50 + 15, (99 - y) * 50 + 21,
                        x * 50 + 23, (99 - y) * 50 + 29);
            } else if (cpaths.charAt(n) == 'D') {
                g.drawLine(x * 50 + 26, (99 - y) * 50 + 21,
                        x * 50 + 34, (99 - y) * 50 + 29);
            }

        }
    }

    private void drawLocked(String lpaths, int x, int y, int z) {
        g.setColor(Color.RED);
        for (int n = 0; n < lpaths.length(); n++) {
            if (lpaths.charAt(n) == 'S') {
                g.drawLine(x * 50 + 20, (99 - y) * 50 + 45,
                        x * 50 + 30, (99 - y) * 50 + 45);
            } else if (lpaths.charAt(n) == 'N') {
                g.drawLine(x * 50 + 20, (99 - y) * 50 + 5,
                        x * 50 + 30, (99 - y) * 50 + 5);
            } else if (lpaths.charAt(n) == 'W') {
                g.drawLine(x * 50 + 5, (99 - y) * 50 + 20,
                        x * 50 + 5, (99 - y) * 50 + 30);
            } else if (lpaths.charAt(n) == 'E') {
                g.drawLine(x * 50 + 45, (99 - y) * 50 + 20,
                        x * 50 + 45, (99 - y) * 50 + 30);
            } else if (lpaths.charAt(n) == 'U') {
                g.drawLine(x * 50 + 23, (99 - y) * 50 + 21,
                        x * 50 + 15, (99 - y) * 50 + 29);
            } else if (lpaths.charAt(n) == 'D') {
                g.drawLine(x * 50 + 34, (99 - y) * 50 + 21,
                        x * 50 + 26, (99 - y) * 50 + 29);
            }
        }
    }

    private boolean saveAs() {
        String filename = filehandling.Saving.savePrompt((Adventure) activeAdv);
        boolean success = filehandling.Saving.saveToFile((Adventure) activeAdv, filename);

        if (success) {
            if (activeAdv.getVersion() > lastVersion) {
                dumpChangeLog();
            }
            printToConsole("File saved successfully.");
            openFile = new File(filename);
            unsavedChanges = false;
        } else {
            printToConsole("File saving aborted.");
        }
        return success;
    }

    private void openFile(String filename, boolean refresh) {
        AdventureEdit tempAdv = (AdventureEdit) filehandling.Opening.readFromFile(filename);

        if (tempAdv != null && tempAdv.getSoftwareVersion() > version) {
            printToConsole("File opening unsuccessful - adventure was created in a more recent version of WITNA. Please obtain a more up-to-date version of WITNA before trying again.");
        } else if (tempAdv != null) {
            activeAdv = tempAdv;

            if (!refresh) {
                console.setText("");
                console.setCaretPosition(0);
                ((AdventureEdit) activeAdv).setSoftwareVersion(version);
                printToConsole("Adventure " + activeAdv.getName() + " opened successfully.");
                clearFields();
            }
            rooms = activeAdv.getRoomManager();

            if (!refresh) {
                floorSpinner.setValue(activeAdv.sz());
                mapScrollPane.getHorizontalScrollBar().setValue(activeAdv.sx() * 50 - mapScrollPane.getWidth() / 2);
                mapScrollPane.getVerticalScrollBar().setValue(5000 - activeAdv.sy() * 50 - mapScrollPane.getHeight() / 2);
                drawFloorToBuffer();
                drawBuffer();
                setTitle("WITNA Editor v" + version + " [" + activeAdv.getName() + "]");
            }
            openFile = new File(filename);
            setUnsavedChanges(false);
            lastVersion = activeAdv.getVersion();

        } else {
            printToConsole("File opening unsuccessful.");
        }
    }

    private void quickSave() {
        if (openFile != null) {
            boolean success = filehandling.Saving.saveToFile((Adventure) activeAdv, openFile.getAbsolutePath());

            if (success) {
                if (activeAdv.getVersion() > lastVersion) {
                    dumpChangeLog();
                }
                printToConsole("File saved successfully.");
                setUnsavedChanges(false);
            } else {
                printToConsole("File saving aborted.");
            }
        } else {
            saveAs();
        }
    }

    private void quitDialog() {
        if (unsavedChanges) {
            Object[] options = {"Save and quit",
                "Quit without saving",
                "Cancel"};
            int n = JOptionPane.showOptionDialog(this,
                    "You have unsaved changes. Would you like to save before you quit?",
                    "WARNING: Unsaved Changes",
                    JOptionPane.YES_NO_CANCEL_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[2]);
            if (n == JOptionPane.NO_OPTION) {
                if (standalone) {
                    System.exit(0);
                } else {
                    dispose();
                }
            } else if (n == JOptionPane.YES_OPTION) {
                if (openFile != null) {
                    quickSave();
                }
                if (saveAs()) {
                    if (standalone) {
                        System.exit(0);
                    } else {
                        dispose();
                    }
                }
            }
        } else {
            dispose();
        }
    }

    private void closeDialog() {
        if (unsavedChanges) {
            Object[] options = {"Save and quit",
                "Quit without saving"};
            int n = JOptionPane.showOptionDialog(this,
                    "You have unsaved changes. Would you like to save before you quit?",
                    "WARNING: Unsaved Changes",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[1]);
            if (n == JOptionPane.NO_OPTION) {
                dispose();
            } else if (n == JOptionPane.YES_OPTION) {
                if (openFile != null) {
                    quickSave();
                }
                if (saveAs()) {
                    dispose();
                }
            }
        } else {
            dispose();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        roomChecksGroup = new javax.swing.ButtonGroup();
        blockedPanel = new javax.swing.JPanel();
        blockedS = new javax.swing.JCheckBox();
        blockedD = new javax.swing.JCheckBox();
        blockedU = new javax.swing.JCheckBox();
        blockedN = new javax.swing.JCheckBox();
        blockedE = new javax.swing.JCheckBox();
        blockedW = new javax.swing.JCheckBox();
        blockedPanel1 = new javax.swing.JPanel();
        blockedS1 = new javax.swing.JCheckBox();
        blockedD1 = new javax.swing.JCheckBox();
        blockedU1 = new javax.swing.JCheckBox();
        blockedN1 = new javax.swing.JCheckBox();
        blockedE1 = new javax.swing.JCheckBox();
        blockedW1 = new javax.swing.JCheckBox();
        copyRadio = new javax.swing.JRadioButton();
        selectRadio = new javax.swing.JRadioButton();
        deleteRadio = new javax.swing.JRadioButton();
        newRadio = new javax.swing.JRadioButton();
        floorSpinner = new javax.swing.JSpinner();
        xField = new javax.swing.JFormattedTextField();
        yField = new javax.swing.JFormattedTextField();
        mapScrollPane = new javax.swing.JScrollPane();
        mapPanel = new MapPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        console = new javax.swing.JTextArea();
        addItemButton = new javax.swing.JButton();
        addMobileButton = new javax.swing.JButton();
        addTriggerButton = new javax.swing.JButton();
        removeItemButton = new javax.swing.JButton();
        removeMobileButton = new javax.swing.JButton();
        removeTriggerButton = new javax.swing.JButton();
        clearButton = new javax.swing.JButton();
        applyButton = new javax.swing.JButton();
        roomControlsSeparator = new javax.swing.JSeparator();
        controlPanelTabPane = new javax.swing.JTabbedPane();
        fieldsScrollPane = new javax.swing.JScrollPane();
        fieldsPanel = new javax.swing.JPanel();
        spawnField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        descriptionScrollPane = new javax.swing.JScrollPane();
        descriptionField = new javax.swing.JTextPane();
        repopField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        repopLabel = new javax.swing.JLabel();
        titleLabel = new javax.swing.JLabel();
        titleBoxScrollPane = new javax.swing.JScrollPane();
        titleField = new javax.swing.JTextPane();
        descriptionVariable = new javax.swing.JLabel();
        goldField = new javax.swing.JFormattedTextField(NumberFormat.getInstance());
        lowLightCheckBox = new javax.swing.JCheckBox();
        lowLightLabel = new javax.swing.JLabel();
        goldLabel = new javax.swing.JLabel();
        spawnLabel = new javax.swing.JLabel();
        closedPanel = new javax.swing.JPanel();
        closedW = new javax.swing.JCheckBox();
        closedD = new javax.swing.JCheckBox();
        closedU = new javax.swing.JCheckBox();
        closedS = new javax.swing.JCheckBox();
        closedE = new javax.swing.JCheckBox();
        closedN = new javax.swing.JCheckBox();
        lockedPanel = new javax.swing.JPanel();
        lockedN = new javax.swing.JCheckBox();
        lockedW = new javax.swing.JCheckBox();
        lockedE = new javax.swing.JCheckBox();
        lockedS = new javax.swing.JCheckBox();
        lockedU = new javax.swing.JCheckBox();
        lockedD = new javax.swing.JCheckBox();
        blockedPanel2 = new javax.swing.JPanel();
        blockedS2 = new javax.swing.JCheckBox();
        blockedD2 = new javax.swing.JCheckBox();
        blockedU2 = new javax.swing.JCheckBox();
        blockedN2 = new javax.swing.JCheckBox();
        blockedE2 = new javax.swing.JCheckBox();
        blockedW2 = new javax.swing.JCheckBox();
        placeablesScrollPane = new javax.swing.JScrollPane();
        placeablesPanel = new javax.swing.JPanel();
        itemsScrollPane = new javax.swing.JScrollPane();
        itemsList = new javax.swing.JList();
        mobileScrollPane = new javax.swing.JScrollPane();
        mobilesList = new javax.swing.JList();
        triggersScrollPane = new javax.swing.JScrollPane();
        triggersList = new javax.swing.JList();
        itemsLabel = new javax.swing.JLabel();
        mobilesLabel = new javax.swing.JLabel();
        triggersLabel = new javax.swing.JLabel();
        menuBar = new javax.swing.JMenuBar();
        fileMenu = new javax.swing.JMenu();
        newAdventureMenuItem = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        saveAsMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        previewFileMenuItem = new javax.swing.JMenuItem();
        fileMenuDump = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        quitMenuItem = new javax.swing.JMenuItem();
        editMenu = new javax.swing.JMenu();
        itemEditorMenuItem = new javax.swing.JMenuItem();
        mobileEditorMenuItem = new javax.swing.JMenuItem();
        triggerEditorMenuItem = new javax.swing.JMenuItem();
        separator2 = new javax.swing.JPopupMenu.Separator();
        vocEditorMenuItem = new javax.swing.JMenuItem();
        skillEditorMenuItem = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        editCurrencyMenuItem = new javax.swing.JMenuItem();
        editSettingsMenuItem = new javax.swing.JMenuItem();
        helpMenu = new javax.swing.JMenu();
        wikiHelpMenuItem = new javax.swing.JMenuItem();
        helpMenuItem = new javax.swing.JMenuItem();

        blockedPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Blocked:"));

        blockedS.setText("S");

        blockedD.setText("D");

        blockedU.setText("U");

        blockedN.setText("N");

        blockedE.setText("E");

        blockedW.setText("W");

        blockedPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Blocked:"));

        blockedS1.setText("S");

        blockedD1.setText("D");

        blockedU1.setText("U");

        blockedN1.setText("N");

        blockedE1.setText("E");

        blockedW1.setText("W");

        javax.swing.GroupLayout blockedPanel1Layout = new javax.swing.GroupLayout(blockedPanel1);
        blockedPanel1.setLayout(blockedPanel1Layout);
        blockedPanel1Layout.setHorizontalGroup(
            blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(blockedPanel1Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(blockedPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(blockedS1)
                            .addComponent(blockedN1))
                        .addGap(0, 0, 0)
                        .addGroup(blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(blockedD1)
                            .addComponent(blockedU1)))
                    .addGroup(blockedPanel1Layout.createSequentialGroup()
                        .addComponent(blockedW1)
                        .addGap(0, 0, 0)
                        .addComponent(blockedE1)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        blockedPanel1Layout.setVerticalGroup(
            blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(blockedPanel1Layout.createSequentialGroup()
                .addGroup(blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blockedU1)
                    .addComponent(blockedN1))
                .addGap(0, 0, 0)
                .addGroup(blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blockedW1)
                    .addComponent(blockedE1))
                .addGap(0, 0, 0)
                .addGroup(blockedPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(blockedS1)
                    .addComponent(blockedD1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout blockedPanelLayout = new javax.swing.GroupLayout(blockedPanel);
        blockedPanel.setLayout(blockedPanelLayout);
        blockedPanelLayout.setHorizontalGroup(
            blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(blockedPanelLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(blockedPanelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(blockedS)
                            .addComponent(blockedN))
                        .addGap(0, 0, 0)
                        .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(blockedD)
                            .addComponent(blockedU)))
                    .addGroup(blockedPanelLayout.createSequentialGroup()
                        .addComponent(blockedW)
                        .addGap(0, 0, 0)
                        .addComponent(blockedE)))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(blockedPanelLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(blockedPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );
        blockedPanelLayout.setVerticalGroup(
            blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(blockedPanelLayout.createSequentialGroup()
                .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blockedU)
                    .addComponent(blockedN))
                .addGap(0, 0, 0)
                .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blockedW)
                    .addComponent(blockedE))
                .addGap(0, 0, 0)
                .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(blockedS)
                    .addComponent(blockedD))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(blockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(blockedPanelLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(blockedPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 0, Short.MAX_VALUE)))
        );

        setDefaultCloseOperation(getCloseOperation());
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(783, 506));

        roomChecksGroup.add(copyRadio);
        copyRadio.setText("Copy..");

        roomChecksGroup.add(selectRadio);
        selectRadio.setSelected(true);
        selectRadio.setText("Select/edit..");

        roomChecksGroup.add(deleteRadio);
        deleteRadio.setText("Delete..");

        roomChecksGroup.add(newRadio);
        newRadio.setText("New..");

        floorSpinner.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        floorSpinner.setModel(new javax.swing.SpinnerNumberModel(1, 0, 99, 1));
        floorSpinner.setEditor(new javax.swing.JSpinner.NumberEditor(floorSpinner, "##"));
        floorSpinner.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                floorSpinnerPropertyChange(evt);
            }
        });

        xField.setColumns(2);
        xField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        yField.setColumns(2);
        yField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        mapScrollPane.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                mapScrollPanePropertyChange(evt);
            }
        });

        mapPanel.setBackground(new java.awt.Color(0, 0, 0));
        mapPanel.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        mapPanel.setForeground(new java.awt.Color(255, 255, 255));
        mapPanel.setToolTipText("");
        mapPanel.setAutoscrolls(true);
        mapPanel.setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));
        mapPanel.setMaximumSize(new java.awt.Dimension(5000, 5000));
        mapPanel.setMinimumSize(new java.awt.Dimension(5000, 5000));
        mapPanel.setPreferredSize(new java.awt.Dimension(5000, 5000));
        mapPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                mapPanelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mapPanelMouseReleased(evt);
            }
        });
        mapPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                mapPanelMouseDragged(evt);
            }
        });

        javax.swing.GroupLayout mapPanelLayout = new javax.swing.GroupLayout(mapPanel);
        mapPanel.setLayout(mapPanelLayout);
        mapPanelLayout.setHorizontalGroup(
            mapPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 4994, Short.MAX_VALUE)
        );
        mapPanelLayout.setVerticalGroup(
            mapPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 4994, Short.MAX_VALUE)
        );

        mapScrollPane.setViewportView(mapPanel);

        console.setEditable(false);
        console.setColumns(20);
        console.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N
        console.setLineWrap(true);
        console.setRows(5);
        console.setToolTipText("Console readout");
        console.setWrapStyleWord(true);
        console.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        jScrollPane1.setViewportView(console);

        addItemButton.setText("+ Item");
        addItemButton.setMinimumSize(new java.awt.Dimension(77, 23));
        addItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemButtonActionPerformed(evt);
            }
        });

        addMobileButton.setText("+ Mobile");
        addMobileButton.setMargin(new java.awt.Insets(2, 12, 2, 12));
        addMobileButton.setMinimumSize(new java.awt.Dimension(77, 23));
        addMobileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addMobileButtonActionPerformed(evt);
            }
        });

        addTriggerButton.setText("+ Trigger");
        addTriggerButton.setMargin(new java.awt.Insets(2, 12, 2, 12));
        addTriggerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addTriggerButtonActionPerformed(evt);
            }
        });

        removeItemButton.setText("- Item");
        removeItemButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        removeItemButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemButtonActionPerformed(evt);
            }
        });

        removeMobileButton.setText("- Mobile");
        removeMobileButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        removeMobileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeMobileButtonActionPerformed(evt);
            }
        });

        removeTriggerButton.setText("- Trigger");
        removeTriggerButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeTriggerButtonActionPerformed(evt);
            }
        });

        clearButton.setText("Clear");
        clearButton.setMargin(new java.awt.Insets(2, 10, 2, 10));
        clearButton.setMaximumSize(new java.awt.Dimension(59, 23));
        clearButton.setMinimumSize(new java.awt.Dimension(59, 23));
        clearButton.setPreferredSize(new java.awt.Dimension(59, 23));
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        applyButton.setText("Update Fields");
        applyButton.setMargin(new java.awt.Insets(2, 8, 2, 8));
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        fieldsScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        fieldsScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        spawnField.setColumns(3);
        spawnField.setText("888");
        spawnField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        descriptionScrollPane.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        descriptionScrollPane.setMaximumSize(new java.awt.Dimension(223, 99));
        descriptionScrollPane.setMinimumSize(new java.awt.Dimension(223, 99));
        descriptionScrollPane.setPreferredSize(new java.awt.Dimension(223, 99));
        descriptionScrollPane.setRequestFocusEnabled(false);

        descriptionField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        descriptionField.setDoubleBuffered(true);
        descriptionField.setMaximumSize(new java.awt.Dimension(223, 97));
        descriptionField.setMinimumSize(new java.awt.Dimension(223, 97));
        descriptionField.setPreferredSize(new java.awt.Dimension(223, 97));
        descriptionScrollPane.setViewportView(descriptionField);

        repopField.setColumns(3);
        repopField.setText("888");
        repopField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        repopLabel.setText("Repop:");

        titleLabel.setText("Title:");

        titleBoxScrollPane.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        titleBoxScrollPane.setMaximumSize(new java.awt.Dimension(223, 38));
        titleBoxScrollPane.setMinimumSize(new java.awt.Dimension(223, 38));
        titleBoxScrollPane.setPreferredSize(new java.awt.Dimension(223, 38));

        titleField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N
        titleField.setDoubleBuffered(true);
        titleField.setMaximumSize(new java.awt.Dimension(223, 36));
        titleField.setMinimumSize(new java.awt.Dimension(223, 36));
        titleField.setPreferredSize(new java.awt.Dimension(223, 36));
        titleBoxScrollPane.setViewportView(titleField);

        descriptionVariable.setText("Description:");

        goldField.setColumns(4);
        goldField.setText("8888");
        goldField.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        lowLightLabel.setText("Dark:");

        goldLabel.setText("Currency:");

        spawnLabel.setText("Spawn:");

        closedPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Closed:"));

        closedW.setText("W");

        closedD.setText("D");

        closedU.setText("U");

        closedS.setText("S");

        closedE.setText("E");

        closedN.setText("N");

        javax.swing.GroupLayout closedPanelLayout = new javax.swing.GroupLayout(closedPanel);
        closedPanel.setLayout(closedPanelLayout);
        closedPanelLayout.setHorizontalGroup(
            closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(closedPanelLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(closedPanelLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(closedN)
                            .addComponent(closedS))
                        .addGap(0, 0, 0)
                        .addGroup(closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(closedD)
                            .addComponent(closedU)))
                    .addGroup(closedPanelLayout.createSequentialGroup()
                        .addComponent(closedW)
                        .addGap(0, 0, 0)
                        .addComponent(closedE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        closedPanelLayout.setVerticalGroup(
            closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(closedPanelLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closedN)
                    .addComponent(closedU))
                .addGap(0, 0, 0)
                .addGroup(closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closedW)
                    .addComponent(closedE))
                .addGap(0, 0, 0)
                .addGroup(closedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closedS)
                    .addComponent(closedD))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lockedPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Locked:"));

        lockedN.setText("N");
        lockedN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockedNActionPerformed(evt);
            }
        });

        lockedW.setText("W");
        lockedW.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockedWActionPerformed(evt);
            }
        });

        lockedE.setText("E");
        lockedE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockedEActionPerformed(evt);
            }
        });

        lockedS.setText("S");
        lockedS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockedSActionPerformed(evt);
            }
        });

        lockedU.setText("U");
        lockedU.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockedUActionPerformed(evt);
            }
        });

        lockedD.setText("D");
        lockedD.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lockedDActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lockedPanelLayout = new javax.swing.GroupLayout(lockedPanel);
        lockedPanel.setLayout(lockedPanelLayout);
        lockedPanelLayout.setHorizontalGroup(
            lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lockedPanelLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lockedPanelLayout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lockedN)
                            .addComponent(lockedS))
                        .addGap(0, 0, 0)
                        .addGroup(lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lockedD)
                            .addComponent(lockedU)))
                    .addGroup(lockedPanelLayout.createSequentialGroup()
                        .addComponent(lockedW)
                        .addGap(0, 0, 0)
                        .addComponent(lockedE)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        lockedPanelLayout.setVerticalGroup(
            lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lockedPanelLayout.createSequentialGroup()
                .addGroup(lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lockedN)
                    .addComponent(lockedU))
                .addGap(0, 0, 0)
                .addGroup(lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lockedW)
                    .addComponent(lockedE))
                .addGap(0, 0, 0)
                .addGroup(lockedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lockedS)
                    .addComponent(lockedD))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        blockedPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Blocked:"));

        blockedS2.setText("S");

        blockedD2.setText("D");

        blockedU2.setText("U");

        blockedN2.setText("N");

        blockedE2.setText("E");

        blockedW2.setText("W");

        javax.swing.GroupLayout blockedPanel2Layout = new javax.swing.GroupLayout(blockedPanel2);
        blockedPanel2.setLayout(blockedPanel2Layout);
        blockedPanel2Layout.setHorizontalGroup(
            blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(blockedPanel2Layout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(blockedPanel2Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(blockedS2)
                            .addComponent(blockedN2))
                        .addGap(0, 0, 0)
                        .addGroup(blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(blockedD2)
                            .addComponent(blockedU2)))
                    .addGroup(blockedPanel2Layout.createSequentialGroup()
                        .addComponent(blockedW2)
                        .addGap(0, 0, 0)
                        .addComponent(blockedE2)))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        blockedPanel2Layout.setVerticalGroup(
            blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(blockedPanel2Layout.createSequentialGroup()
                .addGroup(blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blockedU2)
                    .addComponent(blockedN2))
                .addGap(0, 0, 0)
                .addGroup(blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(blockedW2)
                    .addComponent(blockedE2))
                .addGap(0, 0, 0)
                .addGroup(blockedPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(blockedS2)
                    .addComponent(blockedD2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout fieldsPanelLayout = new javax.swing.GroupLayout(fieldsPanel);
        fieldsPanel.setLayout(fieldsPanelLayout);
        fieldsPanelLayout.setHorizontalGroup(
            fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldsPanelLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addComponent(blockedPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(11, 11, 11)
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(fieldsPanelLayout.createSequentialGroup()
                                .addComponent(spawnLabel)
                                .addGap(18, 18, 18)
                                .addComponent(repopLabel)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(fieldsPanelLayout.createSequentialGroup()
                                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                                        .addGap(7, 7, 7)
                                        .addComponent(spawnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(goldLabel)
                                            .addGroup(fieldsPanelLayout.createSequentialGroup()
                                                .addGap(7, 7, 7)
                                                .addComponent(goldField, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(fieldsPanelLayout.createSequentialGroup()
                                                .addGap(12, 12, 12)
                                                .addComponent(lowLightCheckBox))
                                            .addGroup(fieldsPanelLayout.createSequentialGroup()
                                                .addGap(10, 10, 10)
                                                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                    .addComponent(repopField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addComponent(lowLightLabel))))))
                                .addGap(0, 0, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, fieldsPanelLayout.createSequentialGroup()
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(titleLabel)
                            .addComponent(descriptionVariable))
                        .addContainerGap(177, Short.MAX_VALUE))
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(descriptionScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, fieldsPanelLayout.createSequentialGroup()
                                .addComponent(closedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lockedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(titleBoxScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                        .addGap(0, 28, Short.MAX_VALUE))))
        );
        fieldsPanelLayout.setVerticalGroup(
            fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fieldsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(titleLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleBoxScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(descriptionVariable)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(descriptionScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(closedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lockedPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fieldsPanelLayout.createSequentialGroup()
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spawnLabel)
                            .addComponent(repopLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(spawnField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(repopField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(14, 14, 14)
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(goldLabel)
                            .addComponent(lowLightLabel))
                        .addGap(7, 7, 7)
                        .addGroup(fieldsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(goldField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lowLightCheckBox)))
                    .addComponent(blockedPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        fieldsScrollPane.setViewportView(fieldsPanel);

        controlPanelTabPane.addTab("Fields", fieldsScrollPane);

        placeablesScrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        placeablesScrollPane.setToolTipText("");
        placeablesScrollPane.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

        itemsList.setMaximumSize(new java.awt.Dimension(200, 90));
        itemsList.setMinimumSize(new java.awt.Dimension(200, 90));
        itemsList.setPreferredSize(new java.awt.Dimension(200, 90));
        itemsScrollPane.setViewportView(itemsList);

        mobilesList.setMaximumSize(new java.awt.Dimension(200, 90));
        mobilesList.setMinimumSize(new java.awt.Dimension(200, 90));
        mobilesList.setPreferredSize(new java.awt.Dimension(200, 90));
        mobileScrollPane.setViewportView(mobilesList);

        triggersList.setMaximumSize(new java.awt.Dimension(200, 90));
        triggersList.setMinimumSize(new java.awt.Dimension(200, 90));
        triggersList.setPreferredSize(new java.awt.Dimension(200, 90));
        triggersScrollPane.setViewportView(triggersList);

        itemsLabel.setText("Items:");

        mobilesLabel.setText("Mobiles:");

        triggersLabel.setText("Triggers:");

        javax.swing.GroupLayout placeablesPanelLayout = new javax.swing.GroupLayout(placeablesPanel);
        placeablesPanel.setLayout(placeablesPanelLayout);
        placeablesPanelLayout.setHorizontalGroup(
            placeablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(placeablesPanelLayout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(placeablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(triggersLabel)
                    .addComponent(itemsLabel)
                    .addComponent(mobilesLabel)
                    .addComponent(mobileScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE)
                    .addComponent(itemsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(triggersScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        placeablesPanelLayout.setVerticalGroup(
            placeablesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(placeablesPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(itemsLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itemsScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(mobilesLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mobileScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(triggersLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(triggersScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(26, Short.MAX_VALUE))
        );

        placeablesScrollPane.setViewportView(placeablesPanel);

        controlPanelTabPane.addTab("Placeables", placeablesScrollPane);

        fileMenu.setText("File");
        fileMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuActionPerformed(evt);
            }
        });

        newAdventureMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newAdventureMenuItem.setText("New..");
        newAdventureMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newAdventureMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(newAdventureMenuItem);

        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem.setText("Open..");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(openMenuItem);

        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveMenuItem.setText("Save");
        saveMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveMenuItem);

        saveAsMenuItem.setText("Save as..");
        saveAsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAsMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(saveAsMenuItem);
        fileMenu.add(jSeparator1);

        previewFileMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        previewFileMenuItem.setText("Preview..");
        previewFileMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                previewFileMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(previewFileMenuItem);

        fileMenuDump.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_D, java.awt.event.InputEvent.CTRL_MASK));
        fileMenuDump.setText("Dump change log");
        fileMenuDump.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileMenuDumpActionPerformed(evt);
            }
        });
        fileMenu.add(fileMenuDump);
        fileMenu.add(jSeparator2);

        quitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, java.awt.event.InputEvent.SHIFT_MASK));
        quitMenuItem.setText("Quit..");
        quitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitMenuItemActionPerformed(evt);
            }
        });
        fileMenu.add(quitMenuItem);

        menuBar.add(fileMenu);

        editMenu.setText("Edit");

        itemEditorMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.CTRL_MASK));
        itemEditorMenuItem.setText("Item Editor..");
        itemEditorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemEditorMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(itemEditorMenuItem);

        mobileEditorMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M, java.awt.event.InputEvent.CTRL_MASK));
        mobileEditorMenuItem.setText("Mobile Editor..");
        mobileEditorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mobileEditorMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(mobileEditorMenuItem);

        triggerEditorMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_T, java.awt.event.InputEvent.CTRL_MASK));
        triggerEditorMenuItem.setText("Trigger Editor..");
        triggerEditorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                triggerEditorMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(triggerEditorMenuItem);
        editMenu.add(separator2);

        vocEditorMenuItem.setText("Vocation Editor..");
        vocEditorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vocEditorMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(vocEditorMenuItem);

        skillEditorMenuItem.setText("Skill Editor..");
        skillEditorMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                skillEditorMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(skillEditorMenuItem);
        editMenu.add(jSeparator3);

        editCurrencyMenuItem.setText("Edit Currency..");
        editCurrencyMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCurrencyMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(editCurrencyMenuItem);

        editSettingsMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        editSettingsMenuItem.setText("Edit Settings..");
        editSettingsMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editSettingsMenuItemActionPerformed(evt);
            }
        });
        editMenu.add(editSettingsMenuItem);

        menuBar.add(editMenu);

        helpMenu.setText("Help");

        wikiHelpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        wikiHelpMenuItem.setText("Wiki..");
        wikiHelpMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wikiHelpMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(wikiHelpMenuItem);

        helpMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        helpMenuItem.setText("About..");
        helpMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpMenuItemActionPerformed(evt);
            }
        });
        helpMenu.add(helpMenuItem);

        menuBar.add(helpMenu);

        setJMenuBar(menuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(newRadio)
                            .addComponent(selectRadio))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(copyRadio)
                            .addComponent(deleteRadio)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(xField, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(yField, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(floorSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(20, 20, 20)
                                .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(applyButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(addItemButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(removeItemButton, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(15, 15, 15)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(addMobileButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(removeMobileButton, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(15, 15, 15)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(removeTriggerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(addTriggerButton)))
                            .addComponent(controlPanelTabPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(roomControlsSeparator)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(mapScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(selectRadio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(newRadio))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(copyRadio)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(deleteRadio)))
                        .addGap(9, 9, 9)
                        .addComponent(controlPanelTabPane, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(applyButton)
                            .addComponent(clearButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(floorSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(yField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(roomControlsSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(addItemButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(addMobileButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(addTriggerButton))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(removeItemButton)
                                    .addComponent(removeMobileButton)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(removeTriggerButton))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(mapScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newAdventureMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newAdventureMenuItemActionPerformed
        if (unsavedChanges) {
            Object[] options = {"Proceed", "Cancel"};
            int n = JOptionPane.showOptionDialog(this,
                    "You have unsaved changes. Are you sure you want to start a new adventure?",
                    "WARNING: Unsaved Changes",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[1]);
            if (n == JOptionPane.YES_OPTION) {
                newAdventure();
            }
        } else {
            newAdventure();
        }
    }//GEN-LAST:event_newAdventureMenuItemActionPerformed

    private void fileMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuActionPerformed
    }//GEN-LAST:event_fileMenuActionPerformed

    private void itemEditorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemEditorMenuItemActionPerformed
        PlaceableManager placeables = (PlaceableManager) activeAdv.getPlaceableManager();
        EditorItemView itemEditor = new EditorItemView(this, placeables);
        itemEditor.setVisible(true);
    }//GEN-LAST:event_itemEditorMenuItemActionPerformed

    private void mobileEditorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mobileEditorMenuItemActionPerformed
        PlaceableManager placeables = (PlaceableManager) activeAdv.getPlaceableManager();
        EditorMobileView mobileEditor = new EditorMobileView(this, placeables);
        mobileEditor.setVisible(true);
    }//GEN-LAST:event_mobileEditorMenuItemActionPerformed

    
    private void mapLeftClick(int x, int y, int z) {
        System.out.println("Left click.");
        String mode = getMode();

        if (mode.equals("NEW")) {
            if (newRoom(x, y, z)) {
                selectRoom(x, y, z);
            }
        } else if (mode.equals("SELECT")) {
            selectRoom(x, y, z);
        } else if (mode.equals("DELETE")) {
            deleteRoom(x, y, z);
        } else if (mode.equals("COPY")) {
            copyRoom(x, y, z);
        }
    }

    protected boolean newRoom(int x, int y, int z) {
        if (!rooms.roomExists(x, y, z)) {
            rooms.newRoom(x, y, z);
            getRoomDetails(x, y, z);
            drawFloorToBuffer();
            drawBuffer();
            printToConsole("New room created at (" + x + "," + y + "," + z + ").");
            setUnsavedChanges(true);

            newRadio.setSelected(true);
            return true;
        } else {
            printToConsole("Room already exists at (" + x + "," + y + "," + z + ").");
            selectRadio.setSelected(true);
            selectRoom(x, y, z);

            return false;
        }
    }

    protected void selectRoom(int x, int y, int z) {
        selectRadio.setSelected(true);
        roomSelected = !roomSelected;
        if (roomSelected) {
            // Room was not selected
            if (rooms.roomExists(x, y, z)) {
                printToConsole("Room (" + x + "," + y + "," + z + ") selected.");
                getRoomDetails(x, y, z);
                drawRoomToBuffer(x, y, z);
            } else {
                printToConsole("Room doesn't exist!");
                roomSelected = false;
            }
        } else {
            // Room was selected
            if (rooms.roomExists(x, y, z)) {
                printToConsole("Room (" + x + "," + y + "," + z + ") selected.");
                getRoomDetails(x, y, z);
                roomSelected = true;
            } else {
                printToConsole("Room deselected.");
            }
        }
        drawFloorToBuffer();
        drawBuffer();
    }

    protected void deleteRoom(int x, int y, int z) {
        deleteRadio.setSelected(true);
        if (rooms.roomExists(x, y, z)) {
            rooms.deleteRoom(x, y, z);
            drawFloorToBuffer();
            drawBuffer();
            printToConsole("Room (" + x + "," + y + "," + z + ") deleted.");
            setUnsavedChanges(true);
            if (rooms.sx() == x && rooms.sy() == y && rooms.sz() == z) {
                printToConsole("WARNING: Initial spawn point deleted. Please create a new spawn point.");
            }
        } else {
            printToConsole("Room (" + x + "," + y + "," + z + ") doesn't exist!");
        }
    }

    protected void copyRoom(int x, int y, int z) {
        copyRadio.setSelected(true);
        if (rooms.roomExists(x, y, z)) {
            if (copyRoom[0] == -1) {
                copyRoom[0] = x;
                copyRoom[1] = y;
                copyRoom[2] = z;
                printToConsole("Room (" + x + "," + y + "," + z + ") copied!");
            } else {
                copyRoom[0] = x;
                copyRoom[1] = y;
                copyRoom[2] = z;
                printToConsole("Room (" + x + "," + y + "," + z + ") copied.");
            }
        } else {
            if (copyRoom[0] != -1) {
                rooms.copyRoom(copyRoom[0], copyRoom[1], copyRoom[2], x, y, z);
                drawRoomToBuffer(x, y, z);

                printToConsole("Room (" + copyRoom[0] + "," + copyRoom[1] + ","
                        + copyRoom[2] + ") copied to (" + x + "," + y + "," + z + ").");

                setUnsavedChanges(true);

                copyRoom[0] = -1;
                copyRoom[1] = -1;
                copyRoom[2] = -1;
            } else {
                printToConsole("Room (" + x + "," + y + "," + z + ") doesn't exist!");
            }
        }
        drawFloorToBuffer();
        drawBuffer();
    }

    private void floorSpinnerPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_floorSpinnerPropertyChange
        if (roomSelected) {
            roomSelected = false;
            printToConsole("Room deselected.");
        }
        drawFloorToBuffer();
        drawBuffer();
    }//GEN-LAST:event_floorSpinnerPropertyChange

    int[] dragRoom = new int[3];
    private void updateDragPosition(int x, int y, int z) {
        if(draggingRoom) {
            dragRoom[0] = x;
            dragRoom[1] = y;
            dragRoom[2] = z;
            System.out.println("Updating drag position.");
            drawFloorToBuffer();
            drawBuffer();
            System.out.println("Updated drag position.");
        }
    }
    
    private void saveAsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAsMenuItemActionPerformed
        saveAs();
    }//GEN-LAST:event_saveAsMenuItemActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        if (unsavedChanges) {
            Object[] options = {"Proceed", "Cancel"};
            int n = JOptionPane.showOptionDialog(this,
                    "You have unsaved changes. Are you sure you want to open a new adventure file?",
                    "WARNING: Unsaved Changes",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE,
                    null,
                    options,
                    options[1]);
            if (n == JOptionPane.YES_OPTION) {
                String filename = filehandling.Opening.openPrompt();
                openFile(filename, false);
            }
        } else {
            String filename = filehandling.Opening.openPrompt();
            openFile(filename, false);
        }
    }//GEN-LAST:event_openMenuItemActionPerformed

    private void quitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitMenuItemActionPerformed
        quitDialog();
    }//GEN-LAST:event_quitMenuItemActionPerformed

    private void triggerEditorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_triggerEditorMenuItemActionPerformed
        PlaceableManager placeables = (PlaceableManager) activeAdv.getPlaceableManager();
        SkillManager skills = (SkillManager) activeAdv.getSkillManager();
        EditorTriggerView triggerEditor = new EditorTriggerView(this, placeables, skills);
        triggerEditor.setVisible(true);
    }//GEN-LAST:event_triggerEditorMenuItemActionPerformed

    private void editSettingsMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editSettingsMenuItemActionPerformed
        DialogSettingsView settings = new DialogSettingsView(this, false, activeAdv);
        settings.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosed(java.awt.event.WindowEvent e) {
                setTitle("WITNA Editor v" + version + " [" + activeAdv.getName() + "]");
            }
        });
        settings.setVisible(true);
    }//GEN-LAST:event_editSettingsMenuItemActionPerformed

    private void helpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpMenuItemActionPerformed
        new About(this, false).setVisible(true);
    }//GEN-LAST:event_helpMenuItemActionPerformed

    private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveMenuItemActionPerformed
        quickSave();
    }//GEN-LAST:event_saveMenuItemActionPerformed

    private void removeMobileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeMobileButtonActionPerformed
        if (roomSelected) {
            if (mobilesList.getSelectedIndex() > -1) {
                int x = Integer.parseInt(xField.getText());
                int y = Integer.parseInt(yField.getText());
                int z = Integer.parseInt(floorSpinner.getValue().toString());
                printToConsole("Mobile " + mobilesList.getSelectedValue().toString()
                        + " removed from room (" + x + "," + y + "," + z + ").");
                rooms.removeMobileFromRoomAtIndex(mobilesList.getSelectedIndex(), x, y, z);
                getRoomDetails(x, y, z);
                setUnsavedChanges(true);
            } else {
                printToConsole("Please select a mobile first before attempting to remove it.");
            }
        } else {
            printToConsole("Please select a room first before attempting to modify it.");
        }
}//GEN-LAST:event_removeMobileButtonActionPerformed

    private void removeTriggerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeTriggerButtonActionPerformed
        if (roomSelected) {
            if (triggersList.getSelectedIndex() > -1) {
                int x = Integer.parseInt(xField.getText());
                int y = Integer.parseInt(yField.getText());
                int z = getFloor();
                printToConsole("Trigger " + triggersList.getSelectedValue().toString()
                        + " removed from room (" + x + "," + y + "," + z + ").");
                rooms.removeTriggerFromRoomAtIndex(triggersList.getSelectedIndex(), x, y, z);
                getRoomDetails(x, y, z);
                setUnsavedChanges(true);
            } else {
                printToConsole("Please select a trigger first before attempting to remove it.");
            }
        } else {
            printToConsole("Please select a room first before attempting to modify it.");
        }
}//GEN-LAST:event_removeTriggerButtonActionPerformed

    private void addItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemButtonActionPerformed
        addItem();
}//GEN-LAST:event_addItemButtonActionPerformed
    protected void addItem() {
        if (roomSelected) {
            if (activeAdv.getPlaceableManager().getItem(1).getID() == 0) {
                printToConsole("You need to create an item in the Item Editor before you can place one!");
            } else {
                int x = Integer.parseInt(xField.getText());
                int y = Integer.parseInt(yField.getText());
                int z = Integer.parseInt(floorSpinner.getValue().toString());

                new DialogAddItemView(this, activeAdv, x, y, z).setVisible(true);
            }
        } else {
            printToConsole("Please select a room first before attempting to modify it.");
        }
    }
    private void addMobileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addMobileButtonActionPerformed
        addMobile();
}//GEN-LAST:event_addMobileButtonActionPerformed

    protected void addMobile() {
        if (roomSelected) {
            if (activeAdv.getPlaceableManager().getMobile(1).getID() == 0) {
                printToConsole("You need to create an mobile in the Mobile Editor before you can place one!");
            } else {
                int x = Integer.parseInt(xField.getText());
                int y = Integer.parseInt(yField.getText());
                int z = getFloor();

                new DialogAddMobileView(this, activeAdv, x, y, z).setVisible(true);
            }
        } else {
            printToConsole("Please select a room first before attempting to modify it.");
        }
    }

    private void addTriggerButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addTriggerButtonActionPerformed
        addTrigger();
}//GEN-LAST:event_addTriggerButtonActionPerformed

    protected void addTrigger() {
        if (roomSelected) {
            if (activeAdv.getPlaceableManager().getTrigger(1).getID() == 0) {
                printToConsole("You need to create an trigger in the Trigger Editor before you can place one!");
            } else {
                int x = Integer.parseInt(xField.getText());
                int y = Integer.parseInt(yField.getText());
                int z = Integer.parseInt(floorSpinner.getValue().toString());

                new DialogAddTriggerView(this, activeAdv, x, y, z).setVisible(true);
            }
        } else {
            printToConsole("Please select a room first before attempting to modify it.");
        }
    }

    private void removeItemButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItemButtonActionPerformed
        if (roomSelected) {
            if (itemsList.getSelectedIndex() > -1) {
                int x = Integer.parseInt(xField.getText());
                int y = Integer.parseInt(yField.getText());
                int z = getFloor();
                printToConsole("Item " + itemsList.getSelectedValue().toString()
                        + " removed from room (" + x + "," + y + "," + z + ").");
                rooms.removeItemFromRoomAtIndex(itemsList.getSelectedIndex(), x, y, z);
                getRoomDetails(x, y, z);
                setUnsavedChanges(true);
            } else {
                printToConsole("Please select an item first before attempting to remove it.");
            }
        } else {
            printToConsole("Please select a room first before attempting to modify it.");
        }
}//GEN-LAST:event_removeItemButtonActionPerformed

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        clearFields();
    }//GEN-LAST:event_clearButtonActionPerformed

    private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_applyButtonActionPerformed
        int x = parseInt(xField.getText());
        int y = parseInt(yField.getText());
        int z = parseInt(floorSpinner.getValue().toString());

        if (rooms.roomExists(x, y, z)) {
            updateRoomDetails(x, y, z);
            printToConsole("Room (" + x + "," + y + "," + z + ") updated.");
            setUnsavedChanges(true);
        } else {
            printToConsole("Room (" + x + "," + y + "," + z + ") does not exist!");
        }
    }//GEN-LAST:event_applyButtonActionPerformed

    private void fileMenuDumpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fileMenuDumpActionPerformed
        dumpChangeLog();
    }//GEN-LAST:event_fileMenuDumpActionPerformed

    private void previewFileMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_previewFileMenuItemActionPerformed
        if (openFile == null) {
            printToConsole("You must save your changes before previewing your adventure.");
        }
        quickSave();
        openFile(openFile.getPath(), true);
        try {
            if (roomSelected) {
                AdventurePreview preview = new AdventurePreview(this, false,
                        (Adventure) activeAdv, parseInt(xField.getText()),
                        parseInt(yField.getText()), getFloor());
                preview.setVisible(true);
            } else {
                AdventurePreview preview = new AdventurePreview(this, false,
                        (Adventure) activeAdv);
                preview.setVisible(true);
            }
        } catch (NullPointerException e) {
            e.printStackTrace();;
            printToConsole("You must have at least one room to preview your adventure!");
        }
    }//GEN-LAST:event_previewFileMenuItemActionPerformed

    private void mapScrollPanePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_mapScrollPanePropertyChange
    }//GEN-LAST:event_mapScrollPanePropertyChange

    private void vocEditorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_vocEditorMenuItemActionPerformed
        EditorVocationView vocEditor = new EditorVocationView((java.awt.Frame) this,
                activeAdv.getVocationManager(), (PlaceableManager) activeAdv.getPlaceableManager(),
                activeAdv.getSkillManager());
        vocEditor.setVisible(true);
    }//GEN-LAST:event_vocEditorMenuItemActionPerformed

    private void skillEditorMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skillEditorMenuItemActionPerformed
        EditorSkillView skillEditor = new EditorSkillView(this,
                activeAdv.getSkillManager(), activeAdv.getPlaceableManager());
        skillEditor.setVisible(true);
    }//GEN-LAST:event_skillEditorMenuItemActionPerformed

    private void editCurrencyMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCurrencyMenuItemActionPerformed
        DialogCurrencyView currencyEditor = new DialogCurrencyView(this, activeAdv);
        currencyEditor.setVisible(true);
    }//GEN-LAST:event_editCurrencyMenuItemActionPerformed

    private void lockedNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockedNActionPerformed
       if(lockedN.isSelected()) {
            closedN.setSelected(true);
        }
    }//GEN-LAST:event_lockedNActionPerformed

    private void lockedUActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockedUActionPerformed
        if(lockedU.isSelected()) {
            closedU.setSelected(true);
        }
    }//GEN-LAST:event_lockedUActionPerformed

    private void lockedWActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockedWActionPerformed
        if(lockedW.isSelected()) {
            closedW.setSelected(true);
        }
    }//GEN-LAST:event_lockedWActionPerformed

    private void lockedEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockedEActionPerformed
        if(lockedE.isSelected()) {
            closedE.setSelected(true);
        }
    }//GEN-LAST:event_lockedEActionPerformed

    private void lockedSActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockedSActionPerformed
        if(lockedS.isSelected()) {
            closedS.setSelected(true);
        }
    }//GEN-LAST:event_lockedSActionPerformed

    private void lockedDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lockedDActionPerformed
        if(lockedD.isSelected()) {
            closedD.setSelected(true);
        }
    }//GEN-LAST:event_lockedDActionPerformed

    private void mapPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapPanelMousePressed
        if (!draggingRoom && evt.getButton() == MouseEvent.BUTTON1) {
            dragRoomStart(evt.getX() / 50, 100 - (evt.getY() / 50) - 1, getFloor());
        }
    }//GEN-LAST:event_mapPanelMousePressed

    private void mapPanelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapPanelMouseReleased
        draggingRoom = false;
        if(parseInt(xField.getText()) != dragRoom[0] || parseInt(yField.getText()) != dragRoom[1] || parseInt(floorSpinner.getValue().toString()) != dragRoom[2]) {
            if(((RoomManagerEdit)rooms).moveRoom(parseInt(xField.getText()), parseInt(yField.getText()), parseInt(floorSpinner.getValue().toString()),
                    dragRoom[0], dragRoom[1], dragRoom[2])) {
                 printToConsole("Moved room (" + xField.getText() + "," + yField.getText() + "," + floorSpinner.getValue().toString()
                        + ") to location (" + dragRoom[0] + "," + dragRoom[1] + "," + dragRoom[2] + ").");
                 selectRoom(dragRoom[0], dragRoom[1], dragRoom[2]);
            } else {
                printToConsole("Cannot move room (" + xField.getText() + "," + yField.getText() + "," + floorSpinner.getValue().toString()
                        + ") to location (" + dragRoom[0] + "," + dragRoom[1] + "," + dragRoom[2] + ") - room already exists!");
            }

             drawFloorToBuffer();
             drawBuffer();
        }
    }//GEN-LAST:event_mapPanelMouseReleased

    private void mapPanelMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mapPanelMouseDragged
        int x = evt.getX() / 50;
        int y = 100 - (evt.getY() / 50) - 1;
        int z = getFloor();
        if (!roomSelected) {
            if (!(x + "").equals(xField.getText())
                    || !(y + "").equals(yField.getText())
                    || !(z + "").equals(floorSpinner.getValue().toString())) {
                getRoomDetails(x, y, z);
            }
        }
        System.out.println("Mouse moved. Dragging:" + draggingRoom);
        updateDragPosition(x, y, z);
    }//GEN-LAST:event_mapPanelMouseDragged

    private void wikiHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wikiHelpMenuItemActionPerformed
         openURL("http://help.whatisthynameadventurer.com");
    }//GEN-LAST:event_wikiHelpMenuItemActionPerformed

    private void openURL(String arg) {
        Desktop desktop = Desktop.getDesktop();

        try {
            URI uri = new java.net.URI(arg);
            desktop.browse(uri);
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
    
    boolean draggingRoom = false;
    private void dragRoomStart(int x, int y, int z) {
        if(getMode().equals("SELECT")) {
            selectRoom(x, y, z);
            
            if(roomSelected) {
                draggingRoom = true;
                updateDragPosition(x, y, z);
            }
        }
    }
    
    private void dumpChangeLog() {
        if (openFile != null) {
            if (activeAdv.getVersion() > lastVersion) {
                filehandling.DumpChangeLog.dumpToLogFile((Adventure) activeAdv,
                        openFile.getName().replace(".witml", ""), console.getText());
                console.setText("");
                lastVersion = activeAdv.getVersion();
                printToConsole(activeAdv.getName() + " " + activeAdv.getVersion()
                        + " changes dumped to change log.");
                console.setCaretPosition(0);
            } else {
                printToConsole("You must update the adventure version before dumping to a change log!");
            }
        } else {
            printToConsole("You must save a copy of the adventure before creating a change log!");
        }
    }

    public void printToConsole(String text) {
        console.append(text + "\n");
        console.setCaretPosition(console.getText().length());
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addItemButton;
    private javax.swing.JButton addMobileButton;
    private javax.swing.JButton addTriggerButton;
    private javax.swing.JButton applyButton;
    private javax.swing.JCheckBox blockedD;
    private javax.swing.JCheckBox blockedD1;
    private javax.swing.JCheckBox blockedD2;
    private javax.swing.JCheckBox blockedE;
    private javax.swing.JCheckBox blockedE1;
    private javax.swing.JCheckBox blockedE2;
    private javax.swing.JCheckBox blockedN;
    private javax.swing.JCheckBox blockedN1;
    private javax.swing.JCheckBox blockedN2;
    private javax.swing.JPanel blockedPanel;
    private javax.swing.JPanel blockedPanel1;
    private javax.swing.JPanel blockedPanel2;
    private javax.swing.JCheckBox blockedS;
    private javax.swing.JCheckBox blockedS1;
    private javax.swing.JCheckBox blockedS2;
    private javax.swing.JCheckBox blockedU;
    private javax.swing.JCheckBox blockedU1;
    private javax.swing.JCheckBox blockedU2;
    private javax.swing.JCheckBox blockedW;
    private javax.swing.JCheckBox blockedW1;
    private javax.swing.JCheckBox blockedW2;
    private javax.swing.JButton clearButton;
    private javax.swing.JCheckBox closedD;
    private javax.swing.JCheckBox closedE;
    private javax.swing.JCheckBox closedN;
    private javax.swing.JPanel closedPanel;
    private javax.swing.JCheckBox closedS;
    private javax.swing.JCheckBox closedU;
    private javax.swing.JCheckBox closedW;
    private javax.swing.JTextArea console;
    private javax.swing.JTabbedPane controlPanelTabPane;
    private javax.swing.JRadioButton copyRadio;
    private javax.swing.JRadioButton deleteRadio;
    private javax.swing.JTextPane descriptionField;
    private javax.swing.JScrollPane descriptionScrollPane;
    private javax.swing.JLabel descriptionVariable;
    private javax.swing.JMenuItem editCurrencyMenuItem;
    private javax.swing.JMenu editMenu;
    private javax.swing.JMenuItem editSettingsMenuItem;
    private javax.swing.JPanel fieldsPanel;
    private javax.swing.JScrollPane fieldsScrollPane;
    private javax.swing.JMenu fileMenu;
    private javax.swing.JMenuItem fileMenuDump;
    private javax.swing.JSpinner floorSpinner;
    private javax.swing.JFormattedTextField goldField;
    private javax.swing.JLabel goldLabel;
    private javax.swing.JMenu helpMenu;
    private javax.swing.JMenuItem helpMenuItem;
    private javax.swing.JMenuItem itemEditorMenuItem;
    private javax.swing.JLabel itemsLabel;
    private javax.swing.JList itemsList;
    private javax.swing.JScrollPane itemsScrollPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JCheckBox lockedD;
    private javax.swing.JCheckBox lockedE;
    private javax.swing.JCheckBox lockedN;
    private javax.swing.JPanel lockedPanel;
    private javax.swing.JCheckBox lockedS;
    private javax.swing.JCheckBox lockedU;
    private javax.swing.JCheckBox lockedW;
    private javax.swing.JCheckBox lowLightCheckBox;
    private javax.swing.JLabel lowLightLabel;
    private javax.swing.JPanel mapPanel;
    private javax.swing.JScrollPane mapScrollPane;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenuItem mobileEditorMenuItem;
    private javax.swing.JScrollPane mobileScrollPane;
    private javax.swing.JLabel mobilesLabel;
    private javax.swing.JList mobilesList;
    private javax.swing.JMenuItem newAdventureMenuItem;
    private javax.swing.JRadioButton newRadio;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JPanel placeablesPanel;
    private javax.swing.JScrollPane placeablesScrollPane;
    private javax.swing.JMenuItem previewFileMenuItem;
    private javax.swing.JMenuItem quitMenuItem;
    private javax.swing.JButton removeItemButton;
    private javax.swing.JButton removeMobileButton;
    private javax.swing.JButton removeTriggerButton;
    private javax.swing.JFormattedTextField repopField;
    private javax.swing.JLabel repopLabel;
    private javax.swing.ButtonGroup roomChecksGroup;
    private javax.swing.JSeparator roomControlsSeparator;
    private javax.swing.JMenuItem saveAsMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    private javax.swing.JRadioButton selectRadio;
    private javax.swing.JPopupMenu.Separator separator2;
    private javax.swing.JMenuItem skillEditorMenuItem;
    private javax.swing.JFormattedTextField spawnField;
    private javax.swing.JLabel spawnLabel;
    private javax.swing.JScrollPane titleBoxScrollPane;
    private javax.swing.JTextPane titleField;
    private javax.swing.JLabel titleLabel;
    private javax.swing.JMenuItem triggerEditorMenuItem;
    private javax.swing.JLabel triggersLabel;
    private javax.swing.JList triggersList;
    private javax.swing.JScrollPane triggersScrollPane;
    private javax.swing.JMenuItem vocEditorMenuItem;
    private javax.swing.JMenuItem wikiHelpMenuItem;
    private javax.swing.JFormattedTextField xField;
    private javax.swing.JFormattedTextField yField;
    // End of variables declaration//GEN-END:variables
}
